/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Model;

import javax.microedition.rms.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;

/**
 *
 * @author Pisarchik_S
 */
public class EMailSettings
{

    public EMailSettings()
    {
        Load();
    }
    
    public String m_sSMTPServer = "smtp.mail.ru";
    public String m_sUserName = "Expenses.Tracker@mail.ru";
    public String m_sPassword = "Expenses";
    public String m_sRecipient = "@mail.ru";
    public int m_port = 25;

    private String idRecordsName = "SendMailSettings";

    private void DeleteRecords(String name)
    {
       try
        {
            RecordStore rs = RecordStore.openRecordStore(name, false);
            RecordEnumeration re = rs.enumerateRecords(null, null, false);
            while (re.hasNextElement())
            {
                rs.deleteRecord(re.nextRecordId());
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    public final void Load()
    {
        try
        {
            RecordStore rs = RecordStore.openRecordStore(idRecordsName, false);
            RecordEnumeration re = rs.enumerateRecords(null, null, false);
            if (re.numRecords() > 0)
            {
                byte[] arr = re.nextRecord();
                ByteArrayInputStream inputStream = new ByteArrayInputStream(arr);
                DataInputStream inputDataStream = new DataInputStream(inputStream);

                m_port = inputDataStream.readInt();

                int sz;
                sz = inputDataStream.readInt();
                if (sz > 0)
                {
                    byte [] data = new byte[sz];
                    inputDataStream.read(data);
                    m_sSMTPServer = Encoder.fromBytes(data);
                }

                sz = inputDataStream.readInt();
                if (sz > 0)
                {
                    byte [] data = new byte[sz];
                    inputDataStream.read(data);
                    m_sUserName = Encoder.fromBytes(data);
                }

                sz = inputDataStream.readInt();
                if (sz > 0)
                {
                    byte [] data = new byte[sz];
                    inputDataStream.read(data);
                    m_sPassword = Encoder.fromBytes(data);
                }

                sz = inputDataStream.readInt();
                if (sz > 0)
                {
                    byte [] data = new byte[sz];
                    inputDataStream.read(data);
                    m_sRecipient = Encoder.fromBytes(data);
                }
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

    }

    public void Save()
    {
        try
        {
            DeleteRecords(idRecordsName);

            ByteArrayOutputStream oStream = new ByteArrayOutputStream();
            DataOutputStream oDataStream = new DataOutputStream(oStream);

            oDataStream.writeInt(m_port);

            byte [] arr;

            arr = Encoder.getBytes(m_sSMTPServer);
            oDataStream.writeInt(arr.length);
            if (arr.length > 0)
                oDataStream.write(arr);

            arr = Encoder.getBytes(m_sUserName);
            oDataStream.writeInt(arr.length);
            if (arr.length > 0)
                oDataStream.write(arr);

            arr = Encoder.getBytes(m_sPassword);
            oDataStream.writeInt(arr.length);
            if (arr.length > 0)
                oDataStream.write(arr);

            arr = Encoder.getBytes(m_sRecipient);
            oDataStream.writeInt(arr.length);
            if (arr.length > 0)
                oDataStream.write(arr);

            RecordStore rs = RecordStore.openRecordStore(idRecordsName, true);
            RecordEnumeration re = rs.enumerateRecords(null, null, false);
            byte [] data = oStream.toByteArray();
            rs.addRecord(data, 0, data.length);

        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

    }
}
